package com.shxc.smarttrade.old.presenter;

import android.os.Handler;

import com.shxc.smarttrade.old.bean.OpenTrader;
import com.shxc.smarttrade.old.bean.PendingOrder;
import com.shxc.smarttrade.old.bean.StockChartDatas;
import com.shxc.smarttrade.old.iview.ISimpleTradeFragment;
import com.shxc.smarttrade.old.modal.IRequestServiceListener;
import com.shxc.smarttrade.old.modal.SimpleTradeModal;

/**
 * Created by jcdev1 on 2017/10/27.
 */

public class SimpleTradePresenter {
    private ISimpleTradeFragment iSimpleTradeFragment;
    private SimpleTradeModal modal;
    private Handler handler = new Handler();

    public SimpleTradePresenter(ISimpleTradeFragment iSimpleTradeFragment) {
        this.iSimpleTradeFragment = iSimpleTradeFragment;
        this.modal = new SimpleTradeModal();
    }


    public void getStockChartData(String accessToken, String symbol, String type, final int tag) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (tag != 1)
                    iSimpleTradeFragment.showDialog();
            }
        });
        modal.getStockChartData(accessToken, symbol, type, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iSimpleTradeFragment.dismissDialog();
                        iSimpleTradeFragment.setStockChartData((StockChartDatas) result);
                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iSimpleTradeFragment.dismissDialog();
                        iSimpleTradeFragment.showMyToast(msg);
                    }
                });

            }
        });

    }

    /**
     * open
     *
     * @param symbol
     * @param userId
     * @param cmd
     * @param volume
     * @param tp
     * @param sl
     */
    public void wareHouse(String accessToken, String symbol, int userId, int cmd, float volume, double tp, double sl) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                iSimpleTradeFragment.showDialog();
            }
        });
        modal.wareHouse(accessToken, symbol, userId, cmd, volume, tp, sl, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iSimpleTradeFragment.dismissDialog();
                        iSimpleTradeFragment.setTradeBackInfo((OpenTrader) result);
                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iSimpleTradeFragment.dismissDialog();
                        iSimpleTradeFragment.showMyToast(msg);
                    }
                });
            }
        });
    }

    /**
     * 挂单
     *
     * @param symbol
     * @param userId
     * @param cmd
     * @param volume
     * @param tp
     * @param sl
     * @param pendingPrice
     * @param market
     */
    public void pengingOrder(String accessToken, String symbol, int userId, int cmd, float volume, double tp, double sl, double pendingPrice, double market) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                iSimpleTradeFragment.showDialog();
            }
        });
        modal.pengingOrder(accessToken, symbol, userId, cmd, volume, tp, sl, pendingPrice, market, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iSimpleTradeFragment.dismissDialog();
                        iSimpleTradeFragment.setPendingBackInfo((PendingOrder) result);
                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iSimpleTradeFragment.dismissDialog();
                        iSimpleTradeFragment.showMyToast(msg);
                    }
                });
            }
        });
    }

}
